package com.mybase.app.IO_Net;

import android.app.Activity;
import android.util.Xml;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

/**
 * Created by happy_000 on 2014/12/5.
 */
public class URLActivity extends Activity{

    public void doUILGet() {
    HttpURLConnection urlConnection = null;
    try
    {
        URL url =new URL("http://www.baidu.com") ;
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String result = readInStream(in);
       // handleResult(result);
    }catch(MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch( IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        urlConnection.disconnect();
    }
}
    private String readInStream(InputStream in) {
        Scanner scanner = new Scanner(in).useDelimiter("\\A");
        return scanner.hasNext() ? scanner.next() : "";
    }
    public void doPost(){


    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL("http://www.baidu.com").openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", ("application/xml; charset=utf-8").replaceAll("\\s", ""));
     //   urlConnection.setRequestProperty("Content-Length", String.valueOf(Xml.getBytes().length));
        OutputStream out = urlConnection.getOutputStream();
      //  out.write(Xml.getBytes());
        out.close();
        int responseCode = urlConnection.getResponseCode();
        InputStream in = null;
        if (responseCode == 200) {
            in = new BufferedInputStream(urlConnection.getInputStream());
        } else {
            in = new BufferedInputStream(urlConnection.getErrorStream());
        }
        String result = readInStream(in);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
    }
    }
}
